package com.zj.android.activity

import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.zj.android.BR
import com.zj.android.adapter.PagingAdapter
import com.zj.android.R
import com.zj.android.databinding.ActivityPagingBinding
import com.zj.android.model.PagingViewModel
import com.zj.common.activity.BaseActivity
import com.zj.common.listener.State
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class PagingActivity : BaseActivity<PagingViewModel, ActivityPagingBinding>() {


    private val adapter: PagingAdapter by lazy { PagingAdapter() }


    override fun layoutId(): Int {
        return R.layout.activity_paging
    }

    override fun initData(savedInstanceState: Bundle?) {
        statePager.setRetryClickListener { _, _ ->
            adapter.refresh()
        }
        statePager.showLoading()
        adapter.setOnItemClickListener { _, v, _ ->
            when (v.id) {
                R.id.test -> {
                    Toast.makeText(baseContext, "文字点击事件", Toast.LENGTH_SHORT).show()
                }
                R.id.img -> {
                    Toast.makeText(baseContext, "图片点击事件", Toast.LENGTH_SHORT).show()
                }
                else -> {
                    Toast.makeText(baseContext, "item点击事件", Toast.LENGTH_SHORT).show()
                }
            }
        }

        binding.apply {
            paging.adapter = adapter
            //设置下拉刷新
            srlRefresh.setOnRefreshListener {
                adapter.refresh()
            }
            // 上拉加载更多
            srlRefresh.setOnLoadMoreListener {
                adapter.retry()
            }
            // 根据生命周期获取网络数据
            lifecycleScope.launch {
                viewModel.data.collectLatest {
                    adapter.submitData(it)
                }
            }

            // 请求状态
            adapter.setOnRefreshStateListener {
                when (it) {
                    is State.Loading -> {
                        //如果是手动下拉刷新，则不展示loading页
                        if (srlRefresh.state != RefreshState.Refreshing) {
                            statePager.showLoading()
                        }
                        srlRefresh.resetNoMoreData()
                    }
                    is State.Success -> {
                        statePager.showContent()
                        srlRefresh.finishRefresh(true)
                        srlRefresh.setNoMoreData(it.noMoreData)
                    }
                    is State.Error -> {
                        statePager.showError()
                        srlRefresh.finishRefresh(false)
                    }
                }
            }

            //加载更多状态
            adapter.setOnLoadMoreStateListener {
                when (it) {
                    is State.Loading -> {
                        //重置上拉加载状态，显示加载loading
                        srlRefresh.resetNoMoreData()
                    }
                    is State.Success -> {
                        if (it.noMoreData) {
                            //没有更多了(只能用source的append)
                            srlRefresh.finishLoadMoreWithNoMoreData()
                        } else {
                            srlRefresh.finishLoadMore(true)
                        }
                    }
                    is State.Error -> {
                        srlRefresh.finishLoadMore(false)
                    }
                }
            }
        }
    }

    override fun variableId(): Int {
        return BR.paging
    }
}